﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using ChuanYe.WorkLogManagement.Api.Models;
using ChuanYe.WorkLogManagement.BLL;
using ChuanYe.WorkLogManagement.EF.SqlServer;
using ChuanYe.WorkLogManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.WorkLogManagement.Api.Controllers
{

    /// <summary>
    /// 用户接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        /// <summary>
        /// 增加用户
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<int> Add(RegisterRequest obj)
        {
            ResultDto<int> result = new ResultDto<int>();
            try
            {
                UserInfo info = new UserInfo();
                info.AccountName = obj.AccountName;
                info.Password = obj.Password;
                info.DepartmentID = obj.DepartmentID;
                info.PositionID = obj.PositionID;
                info.RealName = obj.RealName;
                info.MobilePhone = obj.MobilePhone;
                info.Password = obj.Password;
                info.IsManager = obj.IsManager==null?false:obj.IsManager.Value;
                UserInfo exist = null;
                try
                {
                    UserInfoBLL.Instance.Get(0, info.AccountName, string.Empty);
                }
                catch { }
                if (exist != null)
                {
                    result.Status = 0;
                    result.Message = "AccountName Is Exist";
                }
                else
                {
                    var userID = UserInfoBLL.Instance.Add(info);
                    result.FirstParam = userID;
                    result.Status = 1;
                    result.Message = "Requst Is Success";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/User/Add Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Remove(DelUserRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                UserInfoBLL.Instance.Remove(obj.UserID);
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/User/Remove Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 上级用户增加子级用户
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase AddChilderUserID(ChilderUserIDRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                UserInfoBLL.Instance.AddChilderUserID(obj.UserID,obj.ChilderUserIDs);
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/User/AddChilderUserID Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }


        /// <summary>
        /// 上级用户移除子级用户
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase RemoveChilderUserID(ChilderUserIDRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                UserInfoBLL.Instance.RemoveChilderUserID(obj.UserID, obj.ChilderUserIDs);
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/User/RemoveChilderUserID Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }


        /// <summary>
        /// 获取用户下子级用户信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> GetChilderUserInfo(DelUserRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                var list = UserInfoBLL.Instance.GetSubUserInfo(obj.UserID);
                result.FirstParam = list;
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/User/GetSubUserInfo Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 获取没有分配关系的用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> GetNotDistUserInfo()
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                var list = UserInfoBLL.Instance.GetNotDistUserInfo();
                result.FirstParam = list;
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/User/GetNotDistUserInfo Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 查询用户信息 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic,dynamic> GetUserByPage(UserRequest obj)
        {
            ResultDto<dynamic,dynamic> result = new ResultDto<dynamic,dynamic>();
            try
            {
                RequstPageBase page = Pickout.SetPage(obj);
                var list = UserInfoBLL.Instance.GetUserByPage(obj.UserID,obj.DepartmentID,obj.PositionID,obj.IsChilder,page);
                result.FirstParam = list;
                result.SecondParam = page.TotalRows;
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/User/Get Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> Get(DelUserRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                var list = UserInfoBLL.Instance.Get(obj.UserID);
                result.FirstParam = list;
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/User/Get Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 更改用户 密码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase ChangePassword(ChangePasswordRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                var state = UserInfoBLL.Instance.ChangePassword(obj.UserID,obj.OldPassword,obj.NewPassword);
                result.Status = state;
                if (state == -1)
                {
                    result.Message = "UserInfo Not Exist";
                }
                else if (state == 0)
                {
                    result.Message = "Original Password Error";
                }
                else
                {
                    result.Message = "Requst Is Success";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/User/ChangePassword Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage Upload()
        {
            return null;
        }

    }
}